<?php

require_once __DIR__.DS.'blgEntity.php';

class BlgSite extends BlgEntity {
	
	function __construct($guid){
		$this->load($guid);
	}
	
	/**
	 * Loader for this object
	 * @param mixed $data
	 * @param string $type
	 * @throws Exception
	 */
	protected function load($guid){
		$this->initializeAttributes();
		
		$query=	blg_dbschema_query_buid(blg_dbschema_select_sites(array(
			'guid' => $guid
		)));
		$result= blg_dbschema_select($query);						

		if($result->num_rows==1){			
			$this->attributes= $result->{0};
			$this->attributes['url']= add_trainling_slash($this->attributes['url'], '/'); 
			$this->attributes['image']= blg_get_config('site_image_url');
		}else{
			throw new Exception("$guid  is not valid data for loading of a site!");
		}			
	}
	
	/**
	 * (non-PHPdoc)
	 * @see engine/clases/BlgEntity::initializeAttributes()
	 */
	protected function initializeAttributes(){
		parent::initializeAttributes();
		
		$this->attributes['name']= NULL;
		$this->attributes['description']= NULL;
		$this->attributes['url']= NULL;
		$this->attributes['image']= NULL;
	}
	
	/**
	 * Reload object from DB
	 */
	protected function reload(){
		if(!empty($this->attributes['guid'])){
			$this->load($this->attributes['guid']);
			return TRUE;
		}			
		return FALSE;
	}

	/**
	 * Returns the site name
	 */
	public function getName(){
		return $this->attributes['name'];
	}	
	
	/**
	 * Returns the site URL
	 */
	public function getURL(){
		return $this->attributes['url'];
	}
	
	/**
	 * Get site image
	 */
	public function getIcon(){
		return $this->attributes['image'];
	}
}
